package com.tanxiuxin.YiWaiMai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanxiuxin.YiWaiMai.common.Result;
import com.tanxiuxin.YiWaiMai.pojo.Employee;
import com.tanxiuxin.YiWaiMai.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 *   员工管理
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     *    员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request,  @RequestBody Employee employee){

        // 1. 将页面提交的密码 password 进行 MD5 加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 2. 根据页面提交的用户名 username 查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername , employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        // 3. 如果没有查询到则返回登陆失败的结果
        if (emp == null) {
            return Result.error("登陆失败");
        }

        // 4. 密码比对，如果不一致则返回登陆失败的结果
        if (!emp.getPassword().equals(password)) {
            return Result.error("登陆失败");
        }

        // 5. 查看员工状态，如果为禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0){ // 0 表示 禁用， 1 表示未被禁用
            return Result.error("登陆失败，账号已禁用");
        }

        // 6. 登陆成功，将员工 id 存入 Session 并返回登陆成功结果
        request.getSession().setAttribute("employee" , emp.getId());
        return Result.success(emp);
    }

    /**
     *    员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        // 清理 Session 中保存的当前登录员工的 id
        request.removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     *       新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody Employee employee){
        log.info("新增员工，员工信息：{}" , employee.toString());
        // 设置初始密码，需要进行 md5 加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return Result.success("新增员工成功");
    }

    /**
     *   员工信息的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page , int pageSize , String name){
        log.info("page = {} , pageSize = {} , name = {}" , page , pageSize , name);

        // 构造分页构造器
        Page<Employee> pageInfo = new Page<Employee>(page , pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(!StringUtils.isEmpty(name) , Employee::getName , name);
        // 添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        // 执行查询
        employeeService.page(pageInfo , queryWrapper );
        return Result.success(pageInfo);
    }

    /**
     *        根据员工 id 修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return Result.success("员工修改成功");
    }

    /**
     *        根据 id 查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable("id") Long id){
        Employee emp = employeeService.getById(id);
        if (emp != null) {
            return Result.success(emp);
        }
        return Result.error("该员工不存在");
    }
}
